package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AddItemToCartServlet extends HttpServlet {
    //Servlet的功能即负责中转
    //1.处理完请求后的跳转页面
    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String LOGIN = "/WEB-INF/jsp/account/SignOnForm.jsp";

    //2.定义处理该请求所需要的数据
    private String workingItemId;
    private Cart cart;             //购物车
    private LogService logService = new LogService();

    //3.是否需要调用业务逻辑层
    private CatalogService catalogService = new CatalogService();
    private CartService cartService = new CartService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        workingItemId = request.getParameter("workingItemId");

        Account account;
        //从对话中，获取购物车
        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");
        if(cart == null){
            //第一次使用购物车
            cart = new Cart();
        }

        if(account == null){
            session.setAttribute("messageSignOn", "You must sign on before attempting to check out.  Please sign on and try checking out again.");
            request.getRequestDispatcher(LOGIN).forward(request, response);
        }
        else
        {
            cart = cartService.getCartByUsername(account.getUsername());
            if(cart.containsItemId(workingItemId)){
                //已有该物品，数量加一
                //cart.incrementQuantityByItemId(workingItemId);

                HttpServletRequest httpRequest= (HttpServletRequest) request;

                request.getRequestDispatcher(VIEW_CART).forward(request,response);

//                Item item = catalogService.getItem(workingItemId);
            }else{
                Item item = catalogService.getItem(workingItemId);

                cartService.updateCartAddItem(account.getUsername(),item);
                cart = cartService.getCartByUsername(account.getUsername());
                session.setAttribute("cart",cart);
                catalogService = new CatalogService();
                boolean isInStock = catalogService.isItemInStock(workingItemId);

                    HttpServletRequest httpRequest= request;

                    String logInfo = logService.logInfo(" ")   + " 添加物品 " + item + " 到购物车";
                    logService.insertLogInfo(account.getUsername(), logInfo);

//                catalogService.updateCart(account.getUsername(),cart);
                request.getRequestDispatcher(VIEW_CART).forward(request, response);
            }
        }

    }
}
